package com.lifetime.service.impl;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.lifetime.mapper.PicMapper;
import com.lifetime.model.Pic;
import com.lifetime.service.PicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class PicServiceImpl implements PicService {
    @Autowired
    PicMapper picMapper;

    @Override
    public int deletePic(int id) {
        return picMapper.deletePic(id);
    }

    @Override
    public int moveObject(int picId, String TypeId,int user_id) {
        String i=picMapper.selectObject(TypeId);
        if(i==null){
            picMapper.insertObject(TypeId,user_id);
            i=picMapper.selectObject(TypeId);
        }
        return picMapper.moveObject(picId,i,user_id);
    }

    @Override
    public List<Map<String, Object>> getAllPic(int userId) {

        return picMapper.getAllPic(userId);
    }

    @Override
    public     List<Map<Integer, String>> getOtherObject(int userId) {
        return picMapper.getOtherObject(userId);
    }

    @Override
    public List<Map<Integer, String>> getOneObject(String index,int user_id) {
        return picMapper.getOneObject(index,user_id);
    }

    @Override
    public int InstallDescription(Map<String, String> map,int user_id) throws ParseException {
        Pic pic = new Pic();
        pic.setPic_Id(map.get("Id"));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

        /*    Date date = simpleDateFormat.parse(simpleDateFormat.format(map.get("createTime")));*/
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date data = simpleDateFormat.parse(map.get("createTime"));
        System.out.println("时间是："+simpleDateFormat1.format(data));
        pic.setId(Integer.parseInt(map.get("Id")));
        pic.setCreateTime(data);
        System.out.println(picMapper.selectObject(map.get("type_Id")));
        String i=picMapper.selectObject(map.get("type_Id"));
        if(i==null){
            picMapper.insertObject(map.get("type_Id"),user_id);
            i=picMapper.selectObject(map.get("type_Id"));
        }
        pic.setType_Id(i+"");
        pic.setLocation(map.get("location"));
        pic.setDescription(map.get("description"));
        pic.setPic_Id(map.get("type_Id"));
        Timestamp t = new Timestamp(pic.getCreateTime().getTime());
        return picMapper.InstallDescription(pic,t);
    }


    @Override
    public void setOnePic(MultipartFile file, HttpServletRequest request) throws ImageProcessingException, IOException, ParseException {

        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        if (!(suffixName != ".jpg" && suffixName != ".png" && suffixName != ".jpeg")) {
            return;
        }
        String filePath = "E://PicLocation//"; // 上传后的路径
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        fileName = request.getSession().getAttribute("userId") + "_" + simpleDateFormat.format(date) + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }

        String[] array = new String[3];
//如果你对图片的格式有限制，可以直接使用对应格式的Reader如：JPEGImageReader
        ImageMetadataReader.readMetadata(dest)
                .getDirectories().forEach(v ->
                v.getTags().forEach(t -> {
                    //  System.out.println(t.getTagName() + " ： " + t.getDescription());
                    switch (t.getTagName()) {
                        //                    经度
                        case "GPS Longitude":
                            array[0] = t.getDescription();
                            break;
                        //                        纬度
                        case "GPS Latitude":
                            array[1] = t.getDescription();

                            break;
                        //                        拍摄时间
                        case "Date/Time Original":
                            array[2] = t.getDescription();
                            System.out.println(array[2]);
                        default:
                            break;
                    }
                })
        );
        SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss");
        Pic pic = new Pic();
        Date date1 = null;
        if (array[2] == null) {
            date1 = simpleDateFormat1.parse(simpleDateFormat1.format(new Date()));
        } else {

            date1 = simpleDateFormat1.parse(array[2]);
        }

        pic.setCreateTime(date1);
        Timestamp t = new Timestamp(pic.getCreateTime().getTime());
        pic.setPic_Id(fileName);
        pic.setUser_id(Integer.parseInt(String.valueOf(request.getSession().getAttribute("userId"))));
        pic.setType_Id("1");
        pic.setLocation("无");
        pic.setDescription("无");
        picMapper.insertOnePic(pic, t);
    }
}
